using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WozLib.UI.Ink.SketchCommands;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using WozLib.Net;
using WozLib.WozObjects;

namespace WOzTestUI
{
  /// <summary>
  /// Interaction logic for SpeechWindow.xaml
  /// </summary>

  public partial class SpeechWindow : System.Windows.Window
  {
    public static RoutedCommand SpeakCommand;

    public static RoutedCommand PlayCommand;

    public delegate void CommandOccuredDelegate(SketchCommand wozClientCommand);

    public event CommandOccuredDelegate CommandOccured;

    //public ObjectDataProvider StringToSpeakProvider
    //{
    //  get { return _StringToSpeakProvider; }
    //  set { _StringToSpeakProvider = value; }
    //} private ObjectDataProvider _StringToSpeakProvider;

    public ObservableCollection<AudioPrompt> AudioPromptCollection
    {
      get { return _AudioPromptCollection; }
      set { _AudioPromptCollection = value; }
    } private ObservableCollection<AudioPrompt> _AudioPromptCollection;

    static SpeechWindow()
    {
      SpeakCommand = new RoutedCommand("SpeakCommand", typeof(SpeechWindow));
      PlayCommand = new RoutedCommand("PlayCommand", typeof(SpeechWindow));
    }

    public SpeechWindow()
    {

      CommandBinding SpeakCommandCommandCommandBinding = new CommandBinding(SpeakCommand);
      SpeakCommandCommandCommandBinding.CanExecute += new CanExecuteRoutedEventHandler(SpeakCommandCommandCommandBinding_CanExecute);
      SpeakCommandCommandCommandBinding.Executed += new ExecutedRoutedEventHandler(SpeakCommandCommandCommandBinding_Executed);
      CommandBindings.Add(SpeakCommandCommandCommandBinding);

      CommandBinding PlayCommandCommandBinding = new CommandBinding(PlayCommand);
      PlayCommandCommandBinding.CanExecute += new CanExecuteRoutedEventHandler(PlayCommandCommandBinding_CanExecute);
      PlayCommandCommandBinding.Executed += new ExecutedRoutedEventHandler(PlayCommandCommandBinding_Executed);
      CommandBindings.Add(PlayCommandCommandBinding);

      AudioPromptCollection = new ObservableCollection<AudioPrompt>();
      //StringToSpeakProvider = new ObjectDataProvider();
      //StringToSpeakProvider.ObjectInstance = StringToSpeakCollection;

      InitializeComponent();

      this.Loaded += new RoutedEventHandler(SpeechWindow_Loaded);
    }

    void PlayCommandCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      // play the audio file
      try
      {
        if (e.Parameter is AudioToPlay)
        {
          AudioToPlay atp = e.Parameter as AudioToPlay;
          //if (File.Exists(atp.Data))
          //{
          //  System.Media.SoundPlayer s = new System.Media.SoundPlayer(atp.Data);
          //  s.Play();
          //}

          HttpFileServer.RegisterFile(atp.Data);
          //string filename = HttpFileServer.GetRemoteImageUrl(atp.Data);
          string filename = atp.Data;
          PlayAudioSketchCommand wcpac = new PlayAudioSketchCommand(filename);
          NotifyCommandOccured(wcpac);
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

    void PlayCommandCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

    void SpeakCommandCommandCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      if (e.Parameter is StringToSpeak)
      {
        StringToSpeak sts = e.Parameter as StringToSpeak;
        SpeekSketchCommand wcsc = new SpeekSketchCommand(sts.Data);
        NotifyCommandOccured(wcsc);
      }
    }

    void SpeakCommandCommandCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }

    void SpeechWindow_Loaded(object sender, RoutedEventArgs e)
    {
      // setup things in the GUI

      _AddSpeakButton.Click += new RoutedEventHandler(_AddSpeakButton_Click);
      _SpeakButton.Click += new RoutedEventHandler(_SpeakButton_Click);
      _TextToSpeak.AcceptsReturn = false;
      _TextToSpeak.KeyUp += new KeyEventHandler(_TextToSpeak_KeyUp);
      _TextToSpeakList.ItemsReordered += new RoutedEventHandler(_TextToSpeakList_ItemsReordered);
      _OpenAudioButton.Click += new RoutedEventHandler(_OpenAudioButton_Click);
      _AddAudioButton.Click += new RoutedEventHandler(_AddAudioButton_Click);
      _PlayButton.Click += new RoutedEventHandler(_PlayButton_Click);
    }

    void _PlayButton_Click(object sender, RoutedEventArgs e)
    {
      // play the audio file
      try
      {
        //if (File.Exists(_AudioFilename.Text))
        //{
        //  System.Media.SoundPlayer s = new System.Media.SoundPlayer(_AudioFilename.Text);
        //  s.Play();
        //}
        if (_AudioFilename.Text != null && _AudioFilename.Text.Length > 0)
        {
          HttpFileServer.RegisterFile(_AudioFilename.Text);
          //string filename = HttpFileServer.GetRemoteImageUrl(_AudioFilename.Text);
          string filename = _AudioFilename.Text;
          PlayAudioSketchCommand wcpac = new PlayAudioSketchCommand(filename);
          NotifyCommandOccured(wcpac);
        }
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

    void _AddAudioButton_Click(object sender, RoutedEventArgs e)
    {
      // add the audio file to the list of prompts
      if (_AudioFilename.Text != null && _AudioFilename.Text.Length > 0)
      {
        AudioToPlay ata = new AudioToPlay(_AudioFilename.Text);
        AudioPromptCollection.Add(ata);
        _AudioFilename.Text = "";
        ServerObjectRegistry.RegisterObject(ata);
      }
    }

    void _OpenAudioButton_Click(object sender, RoutedEventArgs e)
    {
      // use a file browser to open an audio file
      OpenFileDialog openFileDialog = new OpenFileDialog();
      openFileDialog.Title = "Select an audio file.";
      //openFileDialog.Filter = "Audio Files(*.WAV;*.WMV;*.MP3)|*.WAV;*.WMV;*.MP3|All files (*.*)|*.*";;
      openFileDialog.Filter = "Audio Files(*.WAV)|*.WAV|All files (*.*)|*.*"; ;

      // $tss We need to make sure the server and client registries don't have collisions with these new items

      if (openFileDialog.ShowDialog() == true)
      {
        _AudioFilename.Text = openFileDialog.FileName;
      }
    }

    void _TextToSpeakList_ItemsReordered(object sender, RoutedEventArgs e)
    {
      try
      {
        int selectedIndex = _TextToSpeakList.SelectedIndex;
        ((ObservableCollection<AudioPrompt>)_TextToSpeakList.ItemsSource).Move(_TextToSpeakList.OriginalItemIndex, selectedIndex);
        _TextToSpeakList.SelectedIndex = selectedIndex;
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.ToString());
      }
    }

    void _TextToSpeak_KeyUp(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Enter)
      {
        AddSpeak();
      }
    }

    void _SpeakButton_Click(object sender, RoutedEventArgs e)
    {
      if (_TextToSpeak.Text != null && _TextToSpeak.Text.Length > 0)
      {
        SpeekSketchCommand wcsc = new SpeekSketchCommand(_TextToSpeak.Text);
        NotifyCommandOccured(wcsc);
      }
    }

    void _AddSpeakButton_Click(object sender, RoutedEventArgs e)
    {
      AddSpeak();
    }

    public void AddSpeak()
    {
      if (_TextToSpeak.Text != null && _TextToSpeak.Text.Length > 0)
      {
        StringToSpeak sts = new StringToSpeak(_TextToSpeak.Text);
        AudioPromptCollection.Add(sts);
        _TextToSpeak.Text = "";
        ServerObjectRegistry.RegisterObject(sts);
      }
    }

    private void NotifyCommandOccured(SketchCommand wozClientCreateCommand)
    {
      if (CommandOccured != null)
      {
        CommandOccured(wozClientCreateCommand);
      }
    }

  }

  
}